package cn.yr.service.manage.impl;


import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.RandomUtil;
import cn.yr.domain.entity.Article;
import cn.yr.domain.entity.ArticleTag;
import cn.yr.domain.entity.Tag;
import cn.yr.domain.vo.ArticleVo;
import cn.yr.mapper.ArticleMapper;
import cn.yr.mapper.ArticleTagMapper;
import cn.yr.mapper.TagMapper;
import cn.yr.service.manage.ISysArticleService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Arrays;
import java.util.Date;
import java.util.List;

/**
 * 文章Service业务层处理
 *
 * @author ruoyi
 * @date 2020-03-21
 */
@Service
public class SysArticleServiceImpl implements ISysArticleService {

	@Resource
	private ArticleMapper articleMapper;

	@Resource
	private ArticleTagMapper articleTagMapper;

	@Resource
	private TagMapper tagMapper;

	/**
	 * 查询文章
	 *
	 * @param id 文章ID
	 * @return 文章
	 */
	@Override
	public Article selectArticleById(Long id) {
		return articleMapper.selectById(id.intValue());
	}

	/**
	 * 查询文章列表
	 *
	 * @param article 文章
	 * @return 文章
	 */
	@Override
	public List<Article> selectArticleList(Article article) {
		QueryWrapper<Article> objectQueryWrapper = new QueryWrapper<>();
		objectQueryWrapper.allEq(BeanUtil.beanToMap(article,true,true));
		objectQueryWrapper.orderByDesc("create_time");
		return 	articleMapper.selectList(objectQueryWrapper);
	}


	@Override
	public int insertArticle(ArticleVo articleVo) {
		String[] tags = articleVo.getTags();
		Integer articleId = articleMapper.selectMaxId()+1;
		articleVo.setId(articleId);
		articleVo.setCreateTime(new Date());

		for (int i = 0; i < tags.length; i++) {
			Integer tagId = null;
			//TODO 判断方式要改
			if(_canParseInt(tags[i])){
				tagId = Integer.parseInt(tags[i]);
			}else{
				Tag tag = new Tag();
				//TODO 垃圾代码
				tagId = tagMapper.getMaxId()+1;
				tag.setId(tagId);
				tag.setName(tags[i]);
				tag.setColor(_randomTagColor());
				tag.setCreatetime(new Date());
				tagMapper.insert(tag);
			}
			ArticleTag articleTag = new ArticleTag();
			articleTag.setTagId(tagId);
			articleTag.setArticleId(articleId);
			//TODO 稀碎
			articleTagMapper.insert(articleTag);
		}
		return articleMapper.insert(articleVo);
	}

	public boolean _canParseInt(String str){
		if(str == null){
			return false;
		}
		return str.matches("\\d+");
	}

	public  String _randomTagColor(){
		String[] tagColors= {"default","primary","success","error","warning","volcano",
				"orange","lime","green","cyan","blue","purple"};

		int randomIndex = RandomUtil.randomInt(0,tagColors.length-1);
		return tagColors[randomIndex];
	}

	/**
	 * 新增文章
	 *
	 * @param article 文章
	 * @return 结果
	 */
	@Override
	public int insertArticle(Article article) {
		article.setCreateTime(new Date());
		return articleMapper.insert(article);
	}

	/**
	 * 修改文章
	 *
	 * @param article 文章
	 * @return 结果
	 */
	@Override
	public int updateArticle(Article article) {
		article.setUpdateTime(new Date());
		return articleMapper.updateById(article);
	}

	/**
	 * 批量删除文章
	 *
	 * @param ids 需要删除的文章ID
	 * @return 结果
	 */
	@Override
	public int deleteArticleByIds(Long[] ids) {
		return articleMapper.deleteBatchIds(Arrays.asList(ids));
	}

	/**
	 * 删除文章信息
	 *
	 * @param id 文章ID
	 * @return 结果
	 */
	@Override
	public int deleteArticleById(Long id) {
		return articleMapper.deleteById(id);
	}
}
